var http = require('http');
// Array of type JSON. The content have to be persisted in SAP database.
var arrayJSON = new Array();
var wbsArray = new Array("123", "456", "789");
var wbsArrayYesterday = new Array("222", "333", "444");


// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  console.log("Entering");
  
  if ( request.method === 'POST' ) {

     // the body of the POST is JSON payload.
     var data = '';
     request.on('data', function(chunk) {
                          data += chunk;
                                     });

     request.on('end', function() {
                                                           try 
                                                           {
                                                               data = JSON.parse(data);
                                                           } 
                                                           catch (e) 
                                                           {
                                                               console.log(e);
                                                           } 
                                                           
                                                               if (data.header.cmd === "R"){// Registration
                                                                            saveTimeReg(data, response);
                                                               }
                                                               else if (data.header.cmd === "L"){// Login
                                                                            login(data, response);
                                                               }
                                                               else if (data.header.cmd === "T"){//Test. Shows all elements inside array
                                                            	   			showAllJSON(response);
                                                               }
                                                               else{
                                                            	   response.end;
                                                               }
                                                     }
     );
  }
});

function saveTimeReg(content, response){
    arrayJSON.push(content);
    var output = "";
   
    output += content.id + " *** ";
    output += content.wbs  + " *** ";
    output += content.date  + " *** ";
    output += content.time + " *** ";
    console.log(output);
    var objToJson = {"id":content.id, "status": 200}; 
    writeResponse(response, objToJson);
}
function login(content, response){
			var date = new Date();
			
			var dd = date.getDate().toString();
			var mm = (date.getMonth() + 1);
			var yy = date.getFullYear().toString();
			if ( mm < 10)
				{
				mm = "0" + mm.toString();
				}
			else
				{
				mm = mm.toString();
				}
			
			var today = dd + mm + yy;
			
			date.setDate(date.getDate() -1);
			var dd2 = date.getDate().toString();
			var mm2 = (date.getMonth() + 1);
			var yy2 = date.getFullYear().toString();
			if ( mm2 < 10)
			{
			mm2 = "0" + mm2.toString();
			}
			else
			{
			mm2 = mm2.toString();
			}
			
			var yesterday = dd2 + mm2 + yy2;

			
			
            var objToJson = {"id":content.id, "wbsArrayToday": wbsArray, "wbsArrayYesterday": wbsArrayYesterday, "dateToday": today, "dateYesterday": yesterday}; 
            writeResponse(response, objToJson);

}
function writeResponse(response, jsonObj){
            response.writeHeader(200, {"Content-Type": "Application/json"});
            response.write(JSON.stringify(jsonObj));
            console.log(JSON.stringify(jsonObj));
            response.end();
}
function showAllJSON(response){
			
			
			for(var i=0; i<arrayJSON.length;i++){
				console.log(JSON.stringify(arrayJSON[i]));
			}
			response.end();
}
// Listen on port 8000, IP default
server.listen(8080);

console.log("Server running...");



